/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.tag;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lambach
 */
public class Process_Page {

    public static String divPage(String page, int total,int menu) {
        String str = "";
        int cursor1 = 0;
        try {

            try {
                cursor1 = Integer.parseInt(page);

            } catch (NumberFormatException ex) {
                cursor1 = 1;
            }
            int b = getPage(total);

            if ((cursor1 > 3) && (cursor1 < (b - 3))) {
                if (cursor1 == 4) {
                    str += "<a href=\"?menu="+menu+"&pageno=" + (b - (b - 1)) + "\">First Page </a> . ";

                } else {
                    str += "<a href=\"?menu="+menu+"&pageno=" + (b - (b - 1)) + "\">First Page </a> ... ";
                }
                str += "<a href=\"?menu="+menu+"&pageno=" + (cursor1 - 2) + "\">" + (cursor1 - 2) + "</a> . " + "<a href=\"?menu="+menu+"&pageno=" + (cursor1 - 1) + "\">" + (cursor1 - 1) + "</a> . " + "<a style=\"color:red\">" + cursor1 + "</a> . "
                        + "<a href=\"?menu="+menu+"&pageno=" + (cursor1 + 1) + "\">" + (cursor1 + 1) + "</a> . " + "<a href=\"?menu="+menu+"&pageno=" + (cursor1 + 2) + "\">" + (cursor1 + 2) + "</a> ... " + "<a href=\"?menu="+menu+"&pageno=" + b + "\"> Last Page </a>";
            } else if (cursor1 <= 3) {
                if (b <= 5) {

                    for (int i = 1; i <= b; i++) {
                        if (i == 1) {
                            if (cursor1 == i) {
                                str += "<a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\">First Page </a> . ";
                            } else {
                                str += "<a href=\"?menu="+menu+"&pageno=" + i + "\">First Page </a> . ";
                            }
                        } else if (i == b) {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\"> Last Page </a>";
                            } else {
                                str += " <a href=\"?menu="+menu+"&pageno=" + i + "\"> Last Page </a>";
                            }

                        } else {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\">" + i + "</a> . ";
                            } else {
                                str += " <a href=\"?menu="+menu+"&pageno=" + i + "\">" + i + "</a> . ";
                            }
                        }
                    }
                } else {
                    for (int i = 1; i <= 5; i++) {
                        if (i == 1) {
                            if (cursor1 == i) {
                                str += "<a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\">First Page </a> . ";
                            } else {
                                str += "<a href=\"?menu="+menu+"&pageno=" + i + "\">First Page </a> . ";
                            }

                        } else {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\">" + i + "</a> . ";
                            } else {
                                str += " <a href=\"?menu="+menu+"&pageno=" + i + "\">" + i + "</a> . ";
                            }


                        }
                    }
                    str += " .... <a href=\"?menu="+menu+"&pageno=" + b + "\">  Last Page </a>";

                }
            } else if (cursor1 >= b - 3) {
                if (b <= 5) {

                    for (int i = 1; i <= b; i++) {
                        if (i == 1) {
                            if (cursor1 == i) {
                                str += "<a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\">First Page </a>";
                            } else {
                                str += "<a href=\"?menu="+menu+"&pageno=" + i + "\">First Page </a>";
                            }

                        } else if (i == b) {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\"> Last Page </a>";
                            } else {
                                str += " <a href=\"?menu="+menu+"&pageno=" + i + "\"> Last Page </a>";
                            }

                        } else {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\">" + i + "</a> . ";
                            } else {
                                str += " <a href=\"?menu="+menu+"&pageno=" + i + "\">" + i + "</a> . ";
                            }

                        }
                    }
                } else {
                    str += "<a href=\"?menu="+menu+"&pageno=" + 1 + "\">First Page </a> ... ";
                    for (int i = cursor1 - 2; i <= b; i++) {

                        if (i == b) {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\"> Last Page </a>";
                            } else {
                                str += " <a href=\"?menu="+menu+"&pageno=" + i + "\"> Last Page </a>";
                            }

                        } else {
                            if (cursor1 == i) {
                                str += " <a style=\"color:red\" href=\"?menu="+menu+"&pageno=" + i + "\">" + i + "</a> . ";
                            } else {
                                str += " <a href=\"?menu="+menu+"&pageno=" + i + "\">" + i + "</a> . ";
                            }
                        }
                    }

                }
            }

        } catch (Exception ex) {
            Logger.getLogger(Process_Page.class.getName()).log(Level.SEVERE, null, ex);
        }
        return str;
    }

    public static int getPage(int a) {
        if (a < 30) {
            return 1;
        } else {
            if (a % 10 != 0) {
                return a / 30 + 1;
            } else {
                return a / 30;
            }
        }
    }
}
